using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(Transform))]
public class CustomTransformEditor : Editor
{
    private SerializedProperty positionProperty;
    private SerializedProperty rotationProperty;
    private SerializedProperty scaleProperty;

    private const float fieldWidth = 180f; // 指定输入框的宽度
    private const float buttonWidth = 60f; // 指定重置按钮的宽度

    private bool synchronizePosition = false;
    private bool synchronizeRotation = false;
    private bool synchronizeScale = false;

    private void OnEnable()
    {
        positionProperty = serializedObject.FindProperty("m_LocalPosition");
        rotationProperty = serializedObject.FindProperty("m_LocalRotation");
        scaleProperty = serializedObject.FindProperty("m_LocalScale");
    }

    public override void OnInspectorGUI()
    {
        serializedObject.Update();

        //// 位置  Position
        EditorGUILayout.LabelField("Position", EditorStyles.boldLabel);
        EditorGUI.indentLevel++;
        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.PropertyField(positionProperty, new GUIContent(""), GUILayout.Width(fieldWidth));

        // 绘制同步位置的Checkbox
        synchronizePosition = EditorGUILayout.ToggleLeft("", synchronizePosition, GUILayout.Width(50));

        // 重置按钮
        if (GUILayout.Button("重置", GUILayout.Width(buttonWidth)))
        {
            positionProperty.vector3Value = Vector3.zero;
        }
        EditorGUILayout.EndHorizontal();
        if (synchronizePosition)
        {
            // 获取当前x分量的值，并将其应用到y和z分量
            float xValue = positionProperty.vector3Value.x;
             positionProperty.vector3Value = new Vector3(xValue, xValue, xValue);
        }
        EditorGUI.indentLevel--;
        EditorGUILayout.Space();

        // Rotation
        EditorGUILayout.LabelField("Rotation", EditorStyles.boldLabel);
        EditorGUI.indentLevel++;
        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.PropertyField(rotationProperty, new GUIContent(""), GUILayout.Width(fieldWidth));

        // 绘制同步位置的Checkbox
        synchronizeRotation = EditorGUILayout.ToggleLeft("", synchronizeRotation, GUILayout.Width(50));

        // 重置按钮
        if (GUILayout.Button("重置", GUILayout.Width(buttonWidth)))
        {
            rotationProperty.quaternionValue = Quaternion.identity;
        }

        EditorGUILayout.EndHorizontal();
        if (synchronizeRotation)
        {
            // 获取当前x分量的值，并将其应用到y和z分量
            float xValue = positionProperty.vector3Value.x;
            rotationProperty.quaternionValue = new Quaternion(xValue, xValue, xValue, xValue);
        }
        EditorGUI.indentLevel--;

        // Scale
        EditorGUILayout.LabelField("Scale", EditorStyles.boldLabel);
        EditorGUI.indentLevel++;
        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.PropertyField(scaleProperty, new GUIContent(""), GUILayout.Width(fieldWidth));

        // 绘制同步位置的Checkbox
        synchronizeScale = EditorGUILayout.ToggleLeft("", synchronizeScale, GUILayout.Width(50));

        // 重置按钮
        if (GUILayout.Button("重置", GUILayout.Width(buttonWidth)))
        {
            scaleProperty.vector3Value = Vector3.one;
        }
        EditorGUILayout.EndHorizontal();

        if (synchronizeScale)
        {
            // 获取当前x分量的值，并将其应用到y和z分量
            float xValue = positionProperty.vector3Value.x;
            scaleProperty.vector3Value = new Vector3(xValue, xValue, xValue);
        }
        EditorGUI.indentLevel--;

        serializedObject.ApplyModifiedProperties();
    }
}